﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgileSaude.Entities;
using AgileSaude.Business;
using System.Web.Security;

namespace AgileSaude.Web.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            UsuarioEntities user = new UsuarioEntities();
            user.CPF = txtCPF.Text;

            if (UsuarioFacade.AutenticarLogin(ref user, txtPassword.Text))
            {
                Session["UserLogado"] = user;
                user.DataUltimoAcesso = DateTime.Now;
                new UsuarioFacade().Alterar(user);

                PerfilEntities perfil = new PerfilFacade().GetPerfil(user.idPerfil);
                string sPerfil = "";
                
                if (perfil != null)
                    sPerfil = perfil.Descricao;

                FormsAuthentication.Initialize();

                FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1,
                                                                             user.Nome, DateTime.Now,
                                                                             DateTime.Now.AddMinutes(30), false, sPerfil,
                                                                             FormsAuthentication.FormsCookiePath);
                
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)));
                //Session["Logou"] = true;

                //Response.Redirect(FormsAuthentication.GetRedirectUrl(user.Nome.ToString(), false));
                Response.Redirect("../Geral/MinhasInformacoes.aspx");

                //FormsAuthentication.RedirectFromLoginPage(txtCPF.Text, chkRememberMe.Checked);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg", @"alert('Erro ao fazer login ou Usuário inativo, verifique o usuário e a senha !');", true);
            }
        }

        protected void btnRegistrar_Click(object sender, EventArgs e)
        {
            
        }

    }
}
